package cn.demo.um.support.handler;

import cn.demo.um.domain.UmAttrData;
import cn.demo.um.support.factory.ISearchAttrIndexFactory;
import cn.demo.um.support.model.MatchScoreDto;
import lombok.Data;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Data
public class MatcherContext {
    private Map<String, List<String>> factorMap;
    private String sex;
    private Map<String, MatchScoreDto> matchScoreDtoMap;
    private ISearchAttrIndexFactory searchAttrIndexFactory;

    public MatcherContext(Map<String, List<String>> factorMap, Map<String, MatchScoreDto> matchScoreDtoMap, String sex) {
        this.factorMap = factorMap;
        this.matchScoreDtoMap = matchScoreDtoMap;
        this.sex = sex;
    }

    public List<String> getFactor(String factorCode) {
        return factorMap.get(factorCode);
    }

    public void addMatchScope(List<String> userIds, double score) {
        userIds.forEach(userId -> {
            MatchScoreDto matchScoreDto = matchScoreDtoMap.get(userId);
            if (matchScoreDto == null) {
                matchScoreDto = new MatchScoreDto(userId);
                matchScoreDtoMap.put(userId, matchScoreDto);
            }
            matchScoreDto.setScore(matchScoreDto.getScore() + score);
        });
    }

    public void addMatchScopeAttr(Collection<String> userIds, double score, String code) {
        userIds.forEach(userId -> {
            MatchScoreDto matchScoreDto = matchScoreDtoMap.get(userId);
            if (matchScoreDto == null) {
                matchScoreDto = new MatchScoreDto(userId);
                matchScoreDtoMap.put(userId, matchScoreDto);
            }
            matchScoreDto.setScore(matchScoreDto.getScore() + score);
            matchScoreDto.addFactor(code);
        });
    }
}
